Imports System.Web.Mail

Public Class Class_Mail

    Private objEmail As New MailMessage()
    Private MailSMTPServer As String = ""
    Private MailTo As String = ""
    Private MailCC As String = ""
    Private MailBCC As String = ""
    Private MailFrom As String = ""
    Private MailSubject As String = ""
    Private MailBody As String = ""
    Private MailPriority As MailPriority = System.Web.Mail.MailPriority.High 'High / Low/ Normal
    Private MailBodyFormat As MailFormat = MailFormat.Html 'Html / Text
    Private MailSMTPAuthenType As Integer = 0 '0=None ,1=basic authentication ,2=NTLM
    Private MailSMTPUsername As String = ""
    Private MailSMTPPassword As String = ""
    Private MailSMTPSendUsing As Integer = 2
    Private MailSMTPServerPort As Integer = 25
    Private MailSMTPUseSSL As Boolean = False
    Private MailSMTPConnectionTimeout As Integer = 60

    Public Sub New()
        objEmail = New MailMessage()
    End Sub

    'public property SMTPServer which gets and sets the SMTP Server value
    Public Property Mail_SMTPServer() As String
        Get
            Return MailSMTPServer
        End Get
        Set(ByVal Value As String)
            Value = Value.Trim()
            If Value = "" Then
                Throw New ArgumentNullException
            Else
                MailSMTPServer = Value
            End If
        End Set
    End Property

    'public property TO which gets and sets the mail to value
    Public Property Mail_To() As String
        Get
            Return MailTo
        End Get
        Set(ByVal Value As String)
            Value = Value.Trim()
            If Value = "" Then
                Throw New ArgumentNullException
            Else
                MailTo = Value
            End If
        End Set
    End Property

    'public property CC which gets and sets the mail cc value
    Public Property Mail_CC() As String
        Get
            Return MailCC
        End Get
        Set(ByVal Value As String)
            Value = Value.Trim()
            If Value = "" Then
                Throw New ArgumentNullException
            Else
                MailCC = Value
            End If
        End Set
    End Property

    'public property CC which gets and sets the mail cc value
    Public Property Mail_BCC() As String
        Get
            Return MailBCC
        End Get
        Set(ByVal Value As String)
            Value = Value.Trim()
            If Value = "" Then
                Throw New ArgumentNullException
            Else
                MailBCC = Value
            End If
        End Set
    End Property

    'public property FROM which gets and sets the mail from value
    Public Property Mail_From() As String
        Get
            Return MailFrom
        End Get
        Set(ByVal Value As String)
            Value = Value.Trim()
            If Value = "" Then
                Throw New ArgumentNullException
            Else
                MailFrom = Value
            End If
        End Set
    End Property

    'public property Subject which gets and sets the mail subject value
    Public Property Mail_Subject() As String
        Get
            Return MailSubject
        End Get
        Set(ByVal Value As String)
            Value = Value.Trim()
            If Value = "" Then
                Throw New ArgumentNullException
            Else
                MailSubject = Value
            End If
        End Set
    End Property

    'public property BODY which gets and sets the mail body value
    Public Property Mail_Body() As String
        Get
            Return MailBody
        End Get
        Set(ByVal Value As String)
            Value = Value.Trim()
            If Value = "" Then
                Throw New ArgumentNullException
            Else
                MailBody = Value
            End If
        End Set
    End Property

    'public property Priority which gets and sets the mail Priority value
    Public Property Mail_Priority() As MailPriority
        Get
            Return MailPriority
        End Get
        Set(ByVal Value As MailPriority)
            MailPriority = Value
        End Set
    End Property

    'public property BodyFormat which gets and sets the mail BodyFormat value
    Public Property Mail_BodyFormat() As MailFormat
        Get
            Return MailBodyFormat
        End Get
        Set(ByVal Value As MailFormat)
            MailBodyFormat = Value
        End Set
    End Property

    'public property AuthenType which gets and sets the mail AuthenType value
    Public Property Mail_SMTPAuthenType() As Integer
        Get
            Return MailSMTPAuthenType
        End Get
        Set(ByVal Value As Integer)
            MailSMTPAuthenType = Value
        End Set
    End Property

    'public property SMTPUsername which gets and sets the SMTPUsername value
    Public Property Mail_SMTPUsername() As String
        Get
            Return MailSMTPUsername
        End Get
        Set(ByVal Value As String)
            Value = Value.Trim()
            If Value = "" Then
                Throw New ArgumentNullException
            Else
                MailSMTPUsername = Value
            End If
        End Set
    End Property

    'public property SMTPPassword which gets and sets the SMTPPassword value
    Public Property Mail_SMTPPassword() As String
        Get
            Return MailSMTPPassword
        End Get
        Set(ByVal Value As String)
            Value = Value.Trim()
            If Value = "" Then
                Throw New ArgumentNullException
            Else
                MailSMTPPassword = Value
            End If
        End Set
    End Property

    'public property SendUsing which gets and sets the mail SendUsing value
    Public Property Mail_SendUsing() As Integer
        Get
            Return MailSMTPSendUsing
        End Get
        Set(ByVal Value As Integer)
            MailSMTPSendUsing = Value
        End Set
    End Property

    'public property SMTPServerPort which gets and sets the mail SMTPServerPort value
    Public Property Mail_SMTPServerPort() As Integer
        Get
            Return MailSMTPServerPort
        End Get
        Set(ByVal Value As Integer)
            MailSMTPServerPort = Value
        End Set
    End Property

    'public property SMTPUseSSL which gets and sets the mail SMTPUseSSL value
    Public Property Mail_SMTPUseSSL() As Boolean
        Get
            Return MailSMTPUseSSL
        End Get
        Set(ByVal Value As Boolean)
            MailSMTPUseSSL = Value
        End Set
    End Property

    'public property SMTPConnectionTimeout which gets and sets the mail SMTPConnectionTimeout value
    Public Property Mail_SMTPConnectionTimeout() As Integer
        Get
            Return MailSMTPConnectionTimeout
        End Get
        Set(ByVal Value As Integer)
            MailSMTPConnectionTimeout = Value
        End Set
    End Property

    Public Function Attachment_File(ByVal FileName As String) As Boolean

        Dim Ret As Boolean
        Try
            objEmail.Attachments.Add(New MailAttachment(FileName))
            Ret = True
        Catch exc As Exception
            'Response.Write("Attach failure: " + exc.ToString())
            Ret = False
        End Try
        Attachment_File = Ret

    End Function

    Public Function SendMail() As String

        Dim Ret As String = ""

        objEmail.To = MailTo
        objEmail.From = MailFrom
        objEmail.Cc = MailCC
        objEmail.Bcc = MailBCC
        objEmail.Subject = MailSubject
        objEmail.Body = MailBody
        objEmail.Priority = MailPriority
        objEmail.BodyFormat = MailBodyFormat
        objEmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", MailSMTPUseSSL)

        SmtpMail.SmtpServer = MailSMTPServer

        If (MailSMTPAuthenType <> 0) Then
            objEmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", MailSMTPAuthenType)
            objEmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", MailSMTPUsername)
            objEmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", MailSMTPPassword)
            objEmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", MailSMTPSendUsing)
            objEmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", MailSMTPServerPort)
            objEmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout", MailSMTPConnectionTimeout)
        End If

        Try
            SmtpMail.Send(objEmail)
            Ret = ""
        Catch exc As Exception
            'Response.Write("Send failure: " + exc.ToString())
            Ret = "Send failure: " + exc.ToString()
        End Try
        SendMail = Ret

    End Function



End Class
